package logic

import (
	"context"
	"muke-chain/api/internal/svc"
	"muke-chain/api/internal/types"
	"muke-chain/mukerpc/mukerpcclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddProductLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddProductLogic {
	return &AddProductLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddProductLogic) AddProduct(req *types.AddProductRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	add, err := l.svcCtx.MuKeRpc.AddProduct(l.ctx, &mukerpcclient.AddProductRequest{
		Name:   req.Name,
		Image:  req.Image,
		Number: req.Number,
		Price:  float32(req.Price),
		Owned:  req.Owned,
		UserId: int64(req.UserId),
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Date:    nil,
			Code:    500,
		}, nil
	}
	return &types.Response{
		Message: "商品发布成功",
		Date:    add,
		Code:    200,
	}, nil
}
